<template class="home">
  <div>
    <el-form :rules="rules" ref="loginForm" :model="loginForm" class="loginContainer"
             v-loading="loading" element-loading-text="正在连接外星网络">
      <h3 class="loginTitle">欢迎登陆</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" clearable maxlength="15"
                  placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" show-password clearable maxlength="15"
                  placeholder="请输入密码" @keypress.native.enter="login()"></el-input>
      </el-form-item>
      <el-form-item label="身份选择：">
        <el-col :span="3">
          <el-select v-model="role" placeholder="" style="width: 120px">
            <el-option
              v-for="(item,index) in roles"
              :key="index"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-button type="primary" style="width: 100%" @click="login('loginForm')">登录</el-button>
    </el-form>
  </div>
</template>

<script>
    import {postRequest} from "../utils/api";

    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: '',
                    password: '',
                    code: '',
                    captchaUrl: ''
                },
                role: '1',
                roles: [
                    {
                        value: '0',
                        label: '系统管理员'
                    }, {
                        value: '1',
                        label: '系统用户'
                    }, {
                        value: '2',
                        label: '护工'
                    }, {
                        value: '3',
                        label: '老人'
                    },
                ],
                loading: false,
                rules: {
                    username: [{required: true, message: '用户名不可为空', trigger: 'blur'}],
                    password: [{required: true, message: '密码不可为空', trigger: 'blur'}],
                }
            }
        },
        methods: {
            login() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading = true
                        if (this.role === '0') {
                            postRequest('/manager/login',
                                {
                                    'username': this.loginForm.username,
                                    'password': this.loginForm.password
                                }
                            ).then(res => {
                                // 登录密码校验成功
                                if (res && res.flag === '1') {
                                    this.loading = false
                                    // 保存登录信息
                                    sessionStorage.setItem('isLogin', '1')
                                    this.$store.commit('setLoginParam', res.user)
                                    this.$message.success('❀ ❀ ❀     ' + res.user.username + '   登陆成功！');
                                    // replace 不可回退  铺设可以
                                    this.$router.push('/ManagerHome')
                                } else {
                                    this.loading = false
                                    // 登录失败
                                    this.Manager.password = ''
                                    this.$message.error(res.msg + '!');
                                }
                            })
                        } else if (this.role === '1') {
                            postRequest('/user/login',
                                {
                                    'username': this.loginForm.username,
                                    'password': this.loginForm.password
                                }
                            ).then(res => {
                                // 登录密码校验成功
                                if (res && res.flag === '1') {
                                    this.loading = false
                                    // 保存登录信息
                                    sessionStorage.setItem('isLogin', '1')
                                    this.$store.commit('setLoginParam', res.user)
                                    this.$message.success('❀ ❀ ❀     ' + res.user.username + '   登陆成功！');
                                    // replace 不可回退  铺设可以
                                    this.$router.push('/Home')
                                } else {
                                    this.loading = false
                                    // 登录失败
                                    this.loginForm.password = ''
                                    this.$message.error(res.msg + '!');
                                }
                            })
                        } else if (this.role === '2') {
                            postRequest('/assistant/login',
                                {
                                    'username': this.loginForm.username,
                                    'password': this.loginForm.password
                                }
                            ).then(res => {
                                // 登录密码校验成功
                                if (res && res.flag === '1') {
                                    this.loading = false
                                    // 保存登录信息
                                    sessionStorage.setItem('isLogin', '1')
                                    this.$store.commit('setLoginParam', res.user)
                                    this.$message.success('❀ ❀ ❀     ' + res.user.username + '   登陆成功！');
                                    // replace 不可回退  铺设可以
                                    this.$router.push('/AssisHome')
                                } else {
                                    this.loading = false
                                    // 登录失败
                                    this.assForm.password = ''
                                    this.$message.error(res.msg + '!');
                                }
                            })
                        } else if (this.role === '3') {
                            postRequest('/older/login',
                                {
                                    'username': this.loginForm.username,
                                    'password': this.loginForm.password
                                }
                            ).then(res => {
                                // 登录密码校验成功
                                if (res && res.flag === '1') {
                                    this.loading = false
                                    // 保存登录信息
                                    sessionStorage.setItem('isLogin', '1')
                                    this.$store.commit('setLoginParam', res.user)
                                    this.$message.success('❀ ❀ ❀     ' + res.user.username + '   登陆成功！');
                                    // replace 不可回退  铺设可以
                                    this.$router.push('/OlderHome')
                                } else {
                                    this.loading = false
                                    // 登录失败
                                    this.assForm.password = ''
                                    this.$message.error(res.msg + '!');
                                }
                            })
                        }
                    } else {
                        this.$message.warning('请填写用户名或密码');
                        return false;
                    }
                })
            }
        },
    }
</script>

<style>
  .loginContainer {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 15px 35px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .loginTitle {
    margin: 0px auto 40px auto;
    text-align: center;
  }
  .home {
    position: absolute;
    height: 100%;
    width: 100%;
    background-color: #4d9099;
  }
</style>
